package com.asl.website.banner.controller;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.banner.domain.BannerDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.banner.domain.BizBanner;
import com.asl.website.banner.service.IBizBannerService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * banner图Controller
 * 
 * @author ruoyi
 * @date 2025-05-22
 */
@RestController
@RequestMapping("/banner/banner")
public class BizBannerController extends BaseController
{
    @Autowired
    private IBizBannerService bizBannerService;
    @GetMapping("/content/{categoryId}")
    public AjaxResult getAllBanners(
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage,
            @PathVariable("categoryId") Long categoryId) {

        BizBanner query = new BizBanner();
        if (categoryId != null) {
            query.setCategoryId(categoryId);
        }

        List<BizBanner> banners = bizBannerService.selectBizBannerList(query);

        // 按sort_order排序并转换为DTO列表
        List<BannerDTO> resultList = banners.stream()
                .sorted(Comparator.comparingInt(BizBanner::getSortOrder))
                .map(banner -> {
                    BannerDTO dto = new BannerDTO(banner, acceptLanguage);
                    // 如果不显示按钮，则清空按钮相关字段
                    if (banner.getShowButton() == 0) {
                        dto.setButtonText(null);
                        dto.setButtonLink(null);
                    }
                    return dto;
                })
                .collect(Collectors.toList());

        if (resultList.isEmpty()) {
            return AjaxResult.error("未找到符合条件的轮播图");
        }

        return AjaxResult.success(resultList);
    }

    /**
     * 查询banner图列表
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizBanner bizBanner)
    {
        startPage();
        List<BizBanner> list = bizBannerService.selectBizBannerList(bizBanner);
        return getDataTable(list);
    }

    /**
     * 导出banner图列表
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:export')")
    @Log(title = "banner图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizBanner bizBanner)
    {
        List<BizBanner> list = bizBannerService.selectBizBannerList(bizBanner);
        ExcelUtil<BizBanner> util = new ExcelUtil<BizBanner>(BizBanner.class);
        util.exportExcel(response, list, "banner图数据");
    }

    /**
     * 获取banner图详细信息
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizBannerService.selectBizBannerById(id));
    }

    /**
     * 新增banner图
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:add')")
    @Log(title = "banner图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizBanner bizBanner)
    {
        return toAjax(bizBannerService.insertBizBanner(bizBanner));
    }

    /**
     * 修改banner图
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:edit')")
    @Log(title = "banner图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizBanner bizBanner)
    {
        return toAjax(bizBannerService.updateBizBanner(bizBanner));
    }

    /**
     * 删除banner图
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:remove')")
    @Log(title = "banner图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizBannerService.deleteBizBannerByIds(ids));
    }
}
